package com.yugu.park.system.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageInfo;
import com.yugu.util.SearchPage;
import com.yugu.util.StringUtils;

import cn.hutool.core.lang.UUID;

import com.yugu.park.system.mapper.SysRoleMapper;
import com.yugu.park.dto.TreeNode;
import com.yugu.park.system.entity.SysRoleEntity;
import com.yugu.park.system.service.SysRoleService;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {
      @Resource
	  private SysRoleMapper mapper;
	@Override
	public PageInfo<SysRoleEntity> page(SearchPage<SysRoleEntity> search) {
		PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<SysRoleEntity> list = mapper.page(search.getSearch());
        return PageInfo.of(list);
	}
	@Override
	public void addOrUpdate(SysRoleEntity t) {
		SysRoleEntity old = this.getById(t.getRoleId());
		if(old!=null) {
			//修改
			mapper.deleteRoleAndPersByRoleId(t.getRoleId());
			String status = t.getStatus();
			if(StringUtils.isNotEmpty(status)) {
				old.setStatus(status);
			}
			String roleName = t.getRoleName();
			if(StringUtils.isNotEmpty(roleName)) {
				old.setRoleName(roleName);
			}
			mapper.updateById(old);
		}else {
			//新增
			t.setRoleId(UUID.randomUUID().toString().replaceAll("-", ""));
			mapper.insert(t);
		}
		List<TreeNode> permissions = t.getPermissions();
		if(permissions!=null&&permissions.size()>0) {
			mapper.batchRoleAndPers(t,permissions);
		}
		
	}

}
